package com.chenss.dragonser.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chenss.dragonser.R;
import com.chenss.dragonser.adapter.CouponsAdapter;
import com.chenss.dragonser.adapter.GoodsDetailsAdapter;
import com.chenss.dragonser.app.AppInfo;
import com.chenss.dragonser.app.Constant;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.model.CouponsModel;
import com.chenss.dragonser.model.GoodsDetailsModel;
import com.chenss.dragonser.network.NetworkRequest;
import com.chenss.dragonser.network.RequestCallbcak;
import com.chenss.dragonser.response.CouponAllListResponse;
import com.chenss.dragonser.response.CouponsResponse;
import com.chenss.dragonser.response.GoodsDetailsResponse;
import com.chenss.dragonser.utils.GsonUtil;
import com.chenss.dragonser.utils.ToastUtil;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/***
 * 优惠券页面-------
 *
 */
public class CouponsActivity extends BaseActivity implements RequestCallbcak, BaseRefreshListener {


    LinearLayout ll_left;//左按钮

    TextView tv_page_title;
    ListView mListView;
    List<CouponsModel.StoreCouponBean> list = new ArrayList<>();
    CouponsAdapter listAdapter;

    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装


    PullToRefreshLayout pullToRefreshLayout;

    private int pageNo = 1;//当前页数
    private int totalpage = 0;//总条数
    private final static int REFRESHING = 0x010;
    private final static int LOADMORE = 0x020;
    int freshState = REFRESHING;//默认是刷新
    boolean isEntered = true;
    private String ids;

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_goods_details;
    }

    @Override
    public void initView(View view) {
        ll_left = findView(R.id.ll_left);//左边按钮
        tv_page_title = findView(R.id.tv_page_title);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("已抵扣券");


        pullToRefreshLayout = findView(R.id.pullToRefreshLayout);
        pullToRefreshLayout.setRefreshListener(this);
        mListView = findView(R.id.mListView);

        listAdapter = new CouponsAdapter(this, list);
        mListView.setAdapter(listAdapter);

    }



    @Override
    public void doBusiness(Context mContext) {
        networkRequest = new NetworkRequest(mContext, this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isEntered) {
            isEntered = false;
            refresh();
        }
    }

    /**
     * 获取列表信息
     */
    private void getGoodsListStore(boolean isShow) {
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Store, Constant.classes.Store_an_StoreCumulativeCoupon);
        parmMap.put("storehouse_id", AppInfo.getUser(this).getStorehouse_id());//服务站用户id
//        parmMap.put("storehouse_id", "4");//服务站用户id
//        parmMap.put("sh_goods_id", "8");//服务站用户id
        parmMap.put("psize", "10");
        parmMap.put("p", pageNo + "");
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, isShow, Constant.classes.Store_an_StoreCumulativeCoupon);
    }


    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.ll_left:
                finish();
                break;
        }
    }

    @Override
    public void onFaild(int orderCode, String result) {
        ToastUtil.showShort(this, result);
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.finishRefresh();
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.finishRefresh();
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
            CouponsResponse goodsListResponse = GsonUtil.GsonToBean(result, CouponsResponse.class);
            if (null != goodsListResponse) {

                if (0 == goodsListResponse.getStatus()) {
                    List<CouponsModel.StoreCouponBean> mList = goodsListResponse.getData().getStore_coupon();


                    if (null != mList && mList.size() > 0) {
                        if (freshState == REFRESHING) {
                            list.clear();
                        }
                        list.addAll(mList);

                        listAdapter.setDataList(list);
                        try {
                            totalpage = Integer.parseInt(goodsListResponse.getData().getCount());
                        } catch (NumberFormatException e) {
//                            Log.v(TAG, "返回的总条数不是数字");
                            e.printStackTrace();
                        }
                    } else {
                        if (freshState == REFRESHING) {
                            list.clear();
                            listAdapter.setDataList(null);
                            if (pageNo == 1) {
                                ToastUtil.showShort(this, "暂无数据");
                            }
                        }

                    }

                }


            }


        }
    }

    @Override
    public void refresh() {
        freshState = REFRESHING;
        pageNo = 1;
        getGoodsListStore(false);
    }

    @Override
    public void loadMore() {
        if (pageNo * list.size() < totalpage) {
            freshState = LOADMORE;
            pageNo++;
            getGoodsListStore(false);
        } else {
            pullToRefreshLayout.finishLoadMore();
            ToastUtil.showShort(this, "没有更多数据了");
        }

    }
}
